package com.foxconn.ipebg.common.system.controller;

import ipebg.mzk.common.util.Page;
import ipebg.mzk.facade.entity.MsAdminRole;
import ipebg.mzk.facade.entity.MsAdminRoleAuthority;
import ipebg.mzk.facade.entity.MsAdminUserRole;
import ipebg.mzk.facade.system.bean.MsAdminRoleBean;
import ipebg.mzk.facade.system.service.RoleService;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;


/**
 * 對角色操作方法
 * @author H2602090
 *
 */
@Controller
@RequestMapping("/role.do")
public class RoleController {
	
	 protected Integer rows = 10;
	 protected Integer page = 1;
	 
	 @Resource(name="roleServiceImpl")
	 RoleService roleServiceImpl;
	
	/**
	 * 加載角色頁面方法
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "action=loadRoleInfo")
	public String loadRoleInfo(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "/pages/common/system/roleInfo";
	}
	
	@RequestMapping(params = "action=roleList01")
	public String roleList01(HttpServletRequest request, HttpServletResponse response, Model model) {
		int curPage = 1;
		String page = request.getParameter("curPage");
		if (page != null) {
			curPage = Integer.parseInt(page);
		} 		
		String roleCode = request.getParameter("roleCode");
		String roleName = request.getParameter("roleName");
		MsAdminRole roleBean_1= new MsAdminRole();
		roleBean_1.setRoleCode(roleCode);
		roleBean_1.setRoleName(roleName);
		Page initiontpage = null;
		try {
			initiontpage = roleServiceImpl.getRoleList(roleBean_1,curPage,10);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("initiontpage", initiontpage);
		return "/pages/common/system/roleList";
	}
				
	/**
	 * 查詢角色清單
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(params = "action=roleList")
	public void roleList(HttpServletRequest request, int page,int rows,HttpServletResponse response, Model model) {
		try {

			String roleCode = request.getParameter("roleCode");
			String roleName = request.getParameter("roleName");
			MsAdminRole roleBean_1= new MsAdminRole();
			roleBean_1.setRoleCode(roleCode);
			roleBean_1.setRoleName(roleName);
			Page initiontpage = roleServiceImpl.getRoleList(roleBean_1,page,rows);
			List<MsAdminRole> msAdminRoleList= (List<MsAdminRole>) initiontpage.getResult();
			SimpleDateFormat date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<MsAdminRoleBean>  roleBeanList= new ArrayList<MsAdminRoleBean>();
			for(MsAdminRole bean : msAdminRoleList){
				MsAdminRoleBean roleBean = new MsAdminRoleBean();
				roleBean.setRoleId(bean.getRoleId());
				roleBean.setRoleName(bean.getRoleName());
				roleBean.setRoleCode(bean.getRoleCode());
				roleBean.setDescription(bean.getDescription());
				String createdTime=null;
				if(bean.getCreatedTime() == null){
					createdTime=null;
				}else{
					createdTime=date.format(bean.getCreatedTime());
				}				
				roleBean.setCreatedTime(createdTime);
				String editTime=null;
				if(bean.getEditTime() == null){
					editTime=null;
				}else{
					editTime=date.format(bean.getEditTime());
				}				
				roleBean.setEditTime(editTime);
				roleBeanList.add(roleBean);
			}
								
			Map objMap = new HashMap();
			objMap.put("rows", roleBeanList);
			objMap.put("total", initiontpage.getTotalCount());
			response.getWriter().write(JSON.toJSONString(objMap));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "action=toAddRole")
	public String toAddRole(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		
		return "/pages/common/system/roleAdd";
	}
	/**
	 * 新增角色功能
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(params = "action=roleAdd")
	public void roleAdd(HttpServletRequest request, HttpServletResponse response, Model model) {
		String roleCode = request.getParameter("roleCode");
		String roleName = request.getParameter("roleName");
		String description = request.getParameter("description");
		
		MsAdminRole roleBean = new MsAdminRole();
		roleBean.setRoleCode(roleCode);
		roleBean.setRoleName(roleName);
		roleBean.setDescription(description);
		roleBean.setCreatedTime(new Date());
		
		try {
			roleServiceImpl.saveOrUpdate(roleBean);			
			response.getWriter().write("SUCCESS");
		} catch (IOException e) {
			try {
				response.getWriter().write("ERROR");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 驗證角色代碼是否存在
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(params = "action=checkRoleCode")
	public void checkRoleCode(HttpServletRequest request, HttpServletResponse response, Model model) {
		String roleCode = request.getParameter("roleCode");
		MsAdminRole roleBean = null;
		try {
			roleBean = roleServiceImpl.findMsAdminRoleByRoleCode(roleCode);
			if(roleBean == null){
				response.getWriter().write("SUCCESS");
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "action=toEditRole")
	public String toEditRole(HttpServletRequest request, HttpServletResponse response, Model model) {
		String roleId = request.getParameter("roleId");
		MsAdminRole roleBean = roleServiceImpl.findMsAdminRoleById(roleId);
		request.setAttribute("roleBean", roleBean);
		return "/pages/common/system/roleEdit";
	}
	
	
	
	
	/**
	 * 編輯角色功能
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(params = "action=roleEdit")
	public void roleEdit(HttpServletRequest request, HttpServletResponse response, Model model) {
		String roleId = request.getParameter("roleId");
		String roleCode = request.getParameter("roleCode");
		String roleName = request.getParameter("roleName");
		String description = request.getParameter("description");		
		try {
			MsAdminRole roleBean = roleServiceImpl.findMsAdminRoleById(roleId);
			roleBean.setRoleCode(roleCode);
			roleBean.setRoleName(roleName);
			roleBean.setDescription(description);
			roleServiceImpl.saveOrUpdate(roleBean);
			response.getWriter().write("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 刪除角色功能
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(params = "action=deleteRole")
	public void deleteRole(HttpServletRequest request, HttpServletResponse response, Model model) {
		String roleId = request.getParameter("roleId");
		try {
			List<MsAdminUserRole> userRoleBeanList = roleServiceImpl.findUserByRoleId(roleId);
			List<MsAdminRoleAuthority> roleAuthorityBeanList = roleServiceImpl.findAuthorityByRoleId(roleId);
			if(userRoleBeanList.size() > 0 || roleAuthorityBeanList.size() > 0 ){
				response.getWriter().write("USED");
			}else{
				if(roleServiceImpl.deleteRole(roleId)){
					response.getWriter().write("SUCCESS");
				}else{
					response.getWriter().write("ERROR");
				}
			}						
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
